// UeRegEdit.cpp: implementation of the UeRegEdit class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "UeRegEdit.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

UeRegEdit::UeRegEdit(const CString& strSoftwareKey, HKEY hKey)
{
	g_strSoftwareKey = strSoftwareKey;
	m_hKey = hKey;
}

UeRegEdit::~UeRegEdit()
{
	
}

CString UeRegEdit::GetRegKey(LPCTSTR pszKey, LPCTSTR pszName,const CString Default)
{
	HKEY hKey;
	_TCHAR sv[256];
	unsigned long vtype;
	unsigned long vlen;
	CString subKey=g_strSoftwareKey;
	subKey+=pszKey;
	if(RegOpenKey(m_hKey,subKey,&hKey)!=ERROR_SUCCESS)
	{
		RegCreateKey(m_hKey,subKey,&hKey);
	}
	vlen=256*sizeof(TCHAR);
	if(RegQueryValueEx(hKey,pszName,NULL,&vtype,(LPBYTE)sv,&vlen)!=ERROR_SUCCESS)
	{
		RegSetValueEx(hKey,pszName,NULL,REG_SZ,(LPBYTE)(LPCTSTR)Default,(Default.GetLength()+1)*sizeof(_TCHAR));
		RegCloseKey(hKey);
		return Default;
	}
	CString ret=Default;
	if(vtype==REG_SZ)
		ret=sv;
	ret.TrimLeft();ret.TrimRight();
	if(ret==_T(""))
	{
		RegSetValueEx(hKey,pszName,NULL,REG_SZ,(LPBYTE)(LPCTSTR)Default,(Default.GetLength()+1)*sizeof(_TCHAR));
		ret = Default;
	}
	if(vtype==REG_DWORD)
	{
		DWORD dwVal=*((DWORD*)(void*)sv);
		ret.Format(_T("%d"),dwVal);
	}
	//ret= Default;
	RegCloseKey(hKey);
	return ret;
}

void UeRegEdit::SetRegValue(LPCTSTR pszKey, LPCTSTR pszName, const CString vValue)
{
	HKEY hKey;
	CString subKey=g_strSoftwareKey;
	subKey+=pszKey;
	if(RegOpenKey(m_hKey,subKey,&hKey)!=ERROR_SUCCESS)
	{
		RegCreateKey(m_hKey,subKey,&hKey);
	}
	if(RegSetValueEx(hKey,pszName,NULL,REG_SZ,(LPBYTE)(LPCTSTR)vValue,(vValue.GetLength()+1)*sizeof(TCHAR))!=ERROR_SUCCESS)
	{
		if(::RegDeleteValue(hKey,pszName)==ERROR_SUCCESS)
			RegSetValueEx(hKey,pszName,NULL,REG_SZ,(LPBYTE)(LPCTSTR)vValue,(vValue.GetLength()+1)*sizeof(TCHAR));
	}
	RegCloseKey(hKey);
}

void UeRegEdit::SetRegValue(LPCTSTR pszKey, LPCTSTR pszName, const int vValue)
{
	HKEY hKey;
	CString subKey=g_strSoftwareKey;
	subKey+=pszKey;
	if(RegOpenKey(m_hKey,subKey,&hKey)!=ERROR_SUCCESS)
	{
		RegCreateKey(m_hKey,subKey,&hKey);
	}
	if(RegSetValueEx(hKey,pszName,NULL,REG_DWORD,(unsigned char*)&vValue,sizeof(vValue))!=ERROR_SUCCESS)
	{
		if(::RegDeleteValue(hKey,pszName)==ERROR_SUCCESS)
			RegSetValueEx(hKey,pszName,NULL,REG_DWORD,(unsigned char*)&vValue,sizeof(vValue));
	}
	RegCloseKey(hKey);
}

void UeRegEdit::SetRegValue(LPCTSTR pszKey, LPCTSTR pszName, const float vValue)
{
	CString strTemp;
	strTemp.Format(_T("%f"),vValue);
	SetRegValue(pszKey,pszName,strTemp);
}

void UeRegEdit::SetRegValue(LPCTSTR pszKey, LPCTSTR pszName, const double vValue)
{
	CString strTemp;
	strTemp.Format(_T("%f"),vValue);
	SetRegValue(pszKey,pszName,strTemp);
}

void UeRegEdit::SetRegValue(LPCTSTR pszKey, LPCTSTR pszName, const bool vValue)
{
	DWORD dwVal=(DWORD)vValue;
	SetRegValue(pszKey,pszName,(int)dwVal);
}

long UeRegEdit::GetRegKey(LPCTSTR pszKey, LPCTSTR pszName,const long Default)
{
	CString def;
	def.Format(_T("%d"),Default);
	CString var=GetRegKey(pszKey,pszName,def);
	var.MakeUpper();
	if(var==_T("TRUE"))
		return true;
	else if(var==_T("FALSE"))
		return false;
	return _ttol(var);
}

int UeRegEdit::GetRegKey(LPCTSTR pszKey,LPCTSTR pszName,const int Default)
{
	CString def;
	def.Format(_T("%d"),Default);
	CString var=GetRegKey(pszKey,pszName,def);
	var.MakeUpper();
	if(var==_T("TRUE"))
		return true;
	else if(var==_T("FALSE"))
		return false;
	return _ttoi(var);
}

float UeRegEdit::GetRegKey(LPCTSTR pszKey, LPCTSTR pszName,const float Default)
{
	CString def;
	def.Format(_T("%f"),Default);
	CString var=GetRegKey(pszKey,pszName,def);
	return (float)_tcstod(var,NULL);
}

bool UeRegEdit::GetRegKey(LPCTSTR pszKey, LPCTSTR pszName,const bool Default)
{
	CString var=GetRegKey(pszKey,pszName,Default!=false ? CString(_T("True")) : CString(_T("False")));
	var.MakeUpper();
	if(var==_T("TRUE"))
		return true;
	else if(var==_T("FALSE"))
		return false;
	return _ttoi(var);
}

UeRegEdit::Iterator* UeRegEdit::NewIterator(CString pszKey)
{
	CString strSubKey;

	strSubKey = g_strSoftwareKey+pszKey;

	HKEY hSubKey;
	if( RegOpenKey(m_hKey,strSubKey,&hSubKey) != ERROR_SUCCESS )
	{
		return NULL;
	}
	return new UeRegEdit::Iterator(this,hSubKey);

}

CString UeRegEdit::EnumSubKey(const UINT& nIndex,const HKEY& hKey)
{
	ASSERT(nIndex >= 0);
	ASSERT(hKey);

	CString strSubKeyName;
	TCHAR lpName[256];
	LONG lResult;

	lResult = RegEnumKey(hKey,nIndex,lpName,256);
	if( lResult == ERROR_NO_MORE_ITEMS )
		return CString();
	else if( lResult != ERROR_SUCCESS )
		ASSERT(FALSE);

	strSubKeyName.Format("%s",lpName);

	return strSubKeyName;
}